<?php
/* @var $this QShiftController */
/* @var $userA user->iduser */
/* @var $userB user->iduser */
/* @var $week week number*/
/* @var $year year number*/
/* @var $days array of weekday=>date*/

if (Yii::app()->request->isAjaxRequest){
	Yii::app()->clientScript->scriptMap['jquery.js'] = false;
	Yii::app()->clientScript->scriptMap['jquery.min.js'] = false;	
	Yii::app()->clientScript->scriptMap['jquery.ba-bbq.js'] = false;	
	Yii::app()->clientScript->scriptMap['tableAjaxHandler.js'] = false;	
} 


?>


<center>

<h3>Viewing Week <?php echo $week; ?> -  <?php echo $year; ?>   </h3>


	<?php
	echo CHtml::link('<-Prev Week', 
					 Yii::app()->createUrl('/qshift/swap' , 
										   array('week'=> ($week!=1)? $week-1 : 52, 
											     'year'=> ($week!=1)? $year : $year-1,
												 'userId_A'=>$userA,
												 'userId_B'=>$userB,
												)
										  ), 
					 array('title' => 'Previous Week','style'=>'color:#339900;')
				); 	
	?>
	
	&nbsp | &nbsp
	
	<?php
	echo CHtml::link('Next Week->', 
					 Yii::app()->createUrl('/qshift/swap' , 
										   array('week'=> ($week!=52)? $week+1 : 1, 
												 'year'=>($week!=52)? $year : $year+1,
												 'userId_A' => $userA,
												 'userId_B' => $userB,
												)
										  ), 
					 array('title' => 'Next Week','style'=>'color:#339900;')
				); 	
	?>
	
<br>	
<br>	

<center>

<?php 
	
	//fetch users
	$userA = User::model()->findByPk($userA);
	if($userB!=null)
		$userB = User::model()->findByPk($userB);
	
?>

<!--  RESULT VISOR   -->
<div id="resultVisor"> </div>

<div class="swapTable" >
	<table>
		<tr>
			<th rowspan=2> 
				Weekday
			</th>
			
			<th colspan=2>
				<?php echo $userA->fullname; ?>
			</th>
			
			<?php
			if($userB==null):?>
				
				<th rowspan=9>
					<?php
					
					//DROP DOWN
					echo    CHtml::dropDownList('userSelect',
												 '',
												 CHtml::listData(User::model()->findAll(array('condition'=>'team ='.Yii::app()->user->team_id)),
																							  'iduser',
																							  'fullname'),
												 array(
														'empty'=> 'Select Target User',
														'ajax' => array(
																		'type' => 'GET',
																		'url' => CController::createUrl('qshift/swap'),
																		'data'=> array('week'=>$week,
																					   'year'=>$year,
																					   'userId_A'=>$userA->iduser,
																					   'userId_B'=>'js: $(this).val()',
																					   'isAjaxRequest'=>'1'
																					  ),  
																		'update'=>'#tbl_visor',
																		'id' => 'selectUser-dropdown-'.uniqid()
																	   ),														 
														/*
														'onchange' => 'updateData('. $week . ',' . $year . ',' . $userA->iduser . ', this.value)',
														'submit' => array(
																		'qshift/swap',
																		array('week'=>$week,
																			  'year'=>$year,
																			  'userId_A'=>$userA->iduser,
																			  'userId_B'=>'js: $(this).val()',
																			  'isAjaxRequest'=>'0'
																			), 
																	   ),	
														*/
														
														'id'=> 'dropdown1-link-'.uniqid(),
														'class'=>'dropfullname',
														'style'=>'margin:5px;',
												  )
												);
					?>
				</th>
			
			<?php
			else:?>
				
				<th rowspan=2>
					SWAP!
				</th>
				
				<th colspan=2>
					<?php
					
					//DROP DOWN
					
					echo    CHtml::dropDownList('userSelect',
												 $userB->iduser,
												 CHtml::listData(User::model()->findAll(array('condition'=>'team ='.Yii::app()->user->team_id)),
																							  'iduser',
																							  'fullname'),
												 array(
														'empty'=> 'Select Target User',
														'ajax' => array(
																		'type' => 'GET',
																		'url' => CController::createUrl('qshift/swap'),
																		'data'=> array('week'=>$week,
																					   'year'=>$year,
																					   'userId_A'=>$userA->iduser,
																					   'userId_B'=>'js: $(this).val()',
																					   'isAjaxRequest'=>'1'
																					  ),  
																		'update'=>'#tbl_visor',
																		'id' => 'selectUser2-dropdown-'.uniqid()
																	   ),
														/*
														'submit' => array(
																		'qshift/swap',
																		array('week'=>$week,
																			  'year'=>$year,
																			  'userId_A'=>$userA->iduser,
																			  'userId_B'=>'js: $(this).val()',
																			  'isAjaxRequest'=>'0'
																			), 
																	   ),														
														*/
														'id'=> 'dropdown2-link-'.uniqid(),
														'class'=>'dropfullname',
														'style'=>'margin:5px;',
												  )
												);
					?>
				</th>			
			
			<?php
			endif;?>
			
		</tr>
		
		<tr>
		
			<th> 
				Q-Shift
			</th>
			
			<th> 
				Hours
			</th>		


			
			<?php
			if($userB!=null):?>
				<th>
					Q-Shift
				</th>
			
				<th>
					Hours
				</th>				
			<?php
			endif;?>				
		</tr>
		
		<?php
		foreach ($days as $keyDay=>$date):
		?>
			<tr>
				<td>
					<?php 
					
					echo $keyDay; //print weekday with day number
					
					?>					
				</td>		
									
			<?php
			//fetch userA's qshift array with info
			$criteria= new CDbCriteria();
			$criteria->condition= 'enabled = 1 and iduser = '.$userA->iduser;
			$userAQshift = $this->getTeamQshiftsDate($userA->team, $date, $criteria);
			
			if (!empty($userAQshift)):
			?>
			
				<td>
					<?php
					//print_r($userAQshift);exit;
					foreach($userAQshift as $keyAqshift=>$Aqshift):
						echo $Aqshift['name'];
	
					?>
				</td>	
				
				<td>
					<?php
					
					echo $Aqshift['start_time'].' - '.$Aqshift['end_time'];
					
					endforeach;
					?>
				</td>
				
			<?php
			else:?>	
				<td colspan=2>
					<center><font color=#339900>No Qshift Assigned</font></center>
				</td>						
			<?php
			endif;?>	
			
			<?php
			
			if($userB!=null): // LOAD RIGHT PANE WITH USER SELECTOR?>
			
				<?php
				//fetch userA's qshift array with info
				$criteria= new CDbCriteria();
				$criteria->condition= 'enabled = 1 and iduser = '.$userB->iduser;
				$userBQshift = $this->getTeamQshiftsDate($userB->team, $date, $criteria);
				
				
				if (!empty($userBQshift)):
					
					reset($userAQshift);
					reset($userBQshift);
					
					
					//validate swap option and return array with result=>value and error=>value
					$result = $this->checkSwapOptions($userA->iduser,$userB->iduser,$date);
					//print_r($result);
					
					if($result['result']):
				?>
					<td>
						<?php
						$imghtml=CHtml::image(Yii::app()->theme->baseUrl.'/images/double_arrow.png', 
											  'doSWAP',
											  array(
													'style'=>'width:25px;height:25px;'
													)
											  );
						//echo "userA".$userA->iduser."userB".$userB->iduser."<br>";
						echo CHtml::ajaxLink($imghtml,
											 array('doSwap', 'userA'=>$userA->iduser, 'userB'=>$userB->iduser,'date'=>$date),
											 array('type' => 'POST','update' => '#resultVisor'),
											 array('title'=>$result['message'], 'id' => 'doSwap-link-'.uniqid())
											 );	
						?>
						
					</td>
				
				
				<?php
					else:
				?>	
					<td>
					
						<?php
						$imghtml=CHtml::image(Yii::app()->theme->baseUrl.'/images/cancel.png', 
											  'doSWAP',
											  array(
													'style'=>'width:20px;height:20px;',
													'onclick'=>'alert("'.$result['message'].'");',
													)
											  );
						echo CHtml::link($imghtml, 
										 '#', 
										 array('title'=>$result['message'],'id' => 'cancel-link-'.uniqid()) );
									 
						?>				
					</td>
				
				<?php
					endif;
				?>
					<td>
						<?php
						//print_r($userAQshift);exit;
						foreach($userBQshift as $keyBqshift=>$Bqshift):					
							echo $Bqshift['name'];
		
						?>
					</td>	
					
					<td>
						<?php
						
						echo $Bqshift['start_time'].' - '.$Bqshift['end_time'];
						
						endforeach;
						?>
					</td>
					
				<?php
				else:?>	
					<td colspan=3>
						<center><font color=#339900>No Qshift Assigned</font></center>
					</td>						
				<?php
				endif;?>	
							
			
			<?php
			endif;?>	
			
			</tr>
			
		<?php
		endforeach;
		?>
		
	</table>

</div>